ITI 1120 Fall 2013 - Assignment 3

Available: Feb 23
Due: Friday Mar 8, 22:00, extended till Fri, March 15, 22:00

Instructions

This assignment is to be done individually. Please include your name and student number. Question 1 requires you to design an algorithm in a file A3Q1.doc and Java code in A3Q1.java. Question 2 requires you to implement a Java program in a file A3Q2.java. Question 3 requires a revised A3Q3.java. Zip all the .doc files and the .java file and .class file in a3_xxxxxx.zip, where xxxxxx is your student number, and submit it through the Blackboard Learn.

Marking Scheme (total 100 marks)

Question 1 (40 marks)

A simple method of encoding messages is known as a "substitution cipher".  In this type of cipher, each character in a message is replaced by the corresponding character in a key table. The intention is that only people authorized to read the message know the order of the letters in the key table.

Original

a

b

c

d

e

f

g

h

i

j

k

l

m

n

o

p

q

r

s

t

u

v

w

x

y

z

Code

t

m

e

s

f

k

j

a

x

n

o

v

l

u

c

h

z

g

y

b

w

p

d

r

i

q

Design an algorithm that takes a word (an array of characters) and produces the encoded version of the word (another array of characters) using the above key table to do the encoding.  Here are four examples:

Word to encode:  bonjour
Encoded word:  mcuncwg
 
Word to encode:  cryptographie
Encoded word:  egihbcjgthaxf
 
Word to encode:  security
Encoded word:  yfewgxbi
 
Word to encode:  antidisestablishmentarianism
Encoded word: tubxsxyfybtmvxyalfubtgxtuxyl
 
b. Implement the algorithm from Question 1a as a Java method. In the main method, read a word to encode (as an array of characters), call the encoding method to produce the encoded version of the word (also stored as an array of characters), and print the encoded word. Then repeat, in a loop, reading another word, calling the encoding method, and printing the result, until the user inputs an empty word to encode (presses Enter). 
You can assume that only a single word is entered (lower case letters only; no spaces, punctuation, capitals, digits, etc.). You can use ITI1120.readCharLine() to read the word into an array of characters (do not type any spaces).
 

Question 2 (30 marks) Java implementation

Write a Java program that implements a simple game. The program produces a random number (an integer) between 1 and 10 (inclusively), then it asks the user to guess the number. If the guess is higher than the number, a message is printed "The number you input is too high". If the guess is lower than the number, a message is printed "The number you input is too low". If the guess is equal to the number, a message is printed "You guessed the number" and the program stops. The user is asked to input the guess (in a loop) until the number is guessed.

You can put the game in the main method directly. Aloes include an outer loop that asks the user "Do you want to play again (Y/N)" and it repeats the game if the users types 'Y' or 'y'. For generating a random number you can use Math.random(), that produces a random real number >= 0.0 and < 1, then scale it in the range you need.

 

Question 3 (20 marks) Debugging

The following Java program contains logical errors that you must fix. Logic errors may cause the program not to do what it is supposed to do.  After fixing all errors, save your revised program and include it in your submission.  Explain in comments all the modifications you have made from the original versions of the program.
See the initial Java program in the file A3Q3.java

 

 

Have fun !!!